//
//  CRSpaceFrame.m
//  CRChatRoom
//
//  Created by lanou3g on 15/11/21.
//  Copyright © 2015年 SecondGroup. All rights reserved.
//

#import "CRSpaceFrame.h"
#import "CRSpace.h"
#define margin 5
#define contentFont [UIFont systemFontOfSize:14]
// 显示赞的人的username的label的文字大小
#define praiseInfoFont [UIFont systemFontOfSize:12]

@implementation CRSpaceFrame

- (void)setSpace:(CRSpace *)space
{
    _space = space;
    
    // 根据内容计算控件的frame
    
    CGFloat cellW = [UIScreen mainScreen].bounds.size.width - 2 * margin;
    
    // 头像frame
    CGFloat iconX = margin;
    CGFloat iconY = margin;
    CGFloat iconW = 30;
    CGFloat iconH = iconW;
    _iconF = CGRectMake(iconX, iconY, iconW, iconH);
    
    // 昵称frame
    CGFloat nickX = CGRectGetMaxX(_iconF) + margin;
    CGFloat nickH = 15;
    CGFloat nickY = margin + (iconH - nickH) / 2;
    CGFloat nickW = 100; // 动态计算
    _nickNameF = CGRectMake(nickX, nickY, nickW, nickH);
    
    // 时间的frame
    CGFloat timeH = 20;
    CGFloat timeW = 100;
    CGFloat timeX = 260;
    CGFloat timeY = nickY;
    _timeLabelF = CGRectMake(timeX, timeY, timeW, timeH);

    
    // 正文的frame
    CGFloat contentX = nickX;
    CGFloat contentY = CGRectGetMaxY(_nickNameF) + margin;
    CGFloat contentW = cellW - margin * 2 - iconW * 2;  // 再改
    CGFloat contentH = [self sizeWithText:space.content andMaxSize:CGSizeMake(contentW, MAXFLOAT) andFont:contentFont].height;   // 需要动态计算
    _contentF = CGRectMake(contentX, contentY, contentW, contentH);
    
    if (space.picurl) {
        
        // 图片的frame
        CGFloat imageX = nickX;
        CGFloat imageY = CGRectGetMaxY(_contentF) + margin;
        CGFloat imageW = 100;   // 再改
        CGFloat imageH = imageW;
        _imageF = CGRectMake(imageX, imageY, imageW, imageH);

        // 底部的view
        CGFloat bottomX = margin;
        CGFloat bottomY = CGRectGetMaxY(_imageF) + margin;
        CGFloat bottomW = cellW;
        CGFloat bottomH = 40;
        _bottomViewF = CGRectMake(bottomX, bottomY, bottomW, bottomH);

    } else {

        // 底部的view
        CGFloat bottomX = margin;
        CGFloat bottomY = CGRectGetMaxY(_contentF) + margin;
        CGFloat bottomW = cellW;
        CGFloat bottomH = 40;
        _bottomViewF = CGRectMake(bottomX, bottomY, bottomW, bottomH);
        
     
    }
    
    // 评论条的frame
//    CGFloat conmentX = iconX + margin;
//    CGFloat conmentY = CGRectGetMaxY(_bottomViewF) + margin;
//    CGFloat conmentW = cellW - 2 * conmentX;
//    CGFloat conmentH = 30;
//    _commentTextF = CGRectMake(conmentX, conmentY, conmentW, conmentH);
    
    
    // 显示赞的人的username的label的frame
//    NSMutableString *muStr = [NSMutableString string];
//    for (NSString *str in space.praiseInfo) {
//        [muStr stringByAppendingFormat:@"%@、", str];
//    }
    
    NSArray *strArray = space.praiseInfo;
    NSMutableString *muStr = [NSMutableString string];
    for (NSString *str in strArray) {
        [muStr appendFormat:@"%@、",str];
    }
    NSString *strinfo = [NSString stringWithFormat:@"%@等人觉得很赞", muStr];
    
    CGFloat praiseInfoX = iconX + iconW + margin;
    CGFloat praiseInfoY = CGRectGetMaxY(_bottomViewF) + margin;
    CGFloat praiseInfoW = cellW - praiseInfoX - margin;
    //CGFloat praiseInfoW--动态计算
    CGSize size = [self sizeWithText:strinfo andMaxSize:CGSizeMake(praiseInfoW, MAXFLOAT) andFont:praiseInfoFont];
    CGFloat praiseInfoH = size.height;
    _praiseInfoF = CGRectMake(praiseInfoX, praiseInfoY, praiseInfoW, praiseInfoH);
    
    // 赞的人前面的小图标的frame
    CGFloat praisX = praiseInfoX - margin * 3;
    CGFloat praisY = praiseInfoY;
    CGFloat praisW = 15;
    CGFloat praisH = 15;
    _praiseF = CGRectMake(praisX, praisY, praisW, praisH);
    
    
    // 行高
    _cellHeight = CGRectGetMaxY(_praiseInfoF) + margin + margin;

    
}


// 根据给定的字符串 最大值的size 给定的字体 来计算文字应该占用的大小
- (CGSize)sizeWithText:(NSString *)text andMaxSize:(CGSize)maxSize andFont:(UIFont *)font
{
    NSDictionary *attr = @{NSFontAttributeName : font};
    return [text boundingRectWithSize:maxSize options:NSStringDrawingUsesLineFragmentOrigin attributes:attr context:nil].size;
}



@end











